﻿package com.evidencer.java.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("Server Start....");
		
		// 建立服务器端socket监听
		ServerSocket ss=new ServerSocket(10000);
		// 接收消息
		Socket s=ss.accept();
		// 获得服务器端输入流
		BufferedReader brIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
		String line="";
		System.out.println(s.getLocalAddress().getHostName()+" Say:");
		if((line=brIn.readLine())!=null){
			System.out.println(line);
		}	
		// 写入响应消息
		BufferedWriter bwOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		bwOut.write("Hello!Communication Success!");
		bwOut.flush();
		
		// 关闭连接
		s.close();
		ss.close();
	}

}
